package com.itc.score;

import javax.microedition.rms.RecordEnumeration;
import javax.microedition.rms.RecordStore;
import javax.microedition.rms.RecordStoreException;
import javax.microedition.rms.RecordStoreFullException;
import javax.microedition.rms.RecordStoreNotFoundException;

public class HighScore {
	static String str = null;
	static int best;
	static int score;

	public static int getScore() {
		return score;
	}

	public static void setScore(int score) {
		HighScore.score = score;
	}

	public static String getStr() {
		return str;
	}

	public static void setStr(String str) {
		HighScore.str = str;
	}

	public static int getBest() {
		return best;
	}

	public static void setBest(int best) {
		HighScore.best = best;
	}

	public static void writeHighScore(String scoree)
			throws RecordStoreFullException, RecordStoreNotFoundException,
			RecordStoreException {
		// We open the recordstore
		RecordStore highscore = RecordStore.openRecordStore("High Score", true);

		// To add a new HiScore we use a quick string comma-separated
		String str = new String(scoree);
		byte[] strb = str.getBytes();
		int id = highscore.addRecord(strb, 0, strb.length);

		highscore.closeRecordStore();
	}

	public static void readHighScore() throws RecordStoreFullException,
			RecordStoreNotFoundException, RecordStoreException {
		RecordStore highscore = RecordStore.openRecordStore("High Score", true);
		// To read all hiscores saved on the RMS
		RecordEnumeration enum = highscore.enumerateRecords(null, null, false);
		int id;
		byte[] record;

		while (enum.hasNextElement()) {
			id = enum.nextRecordId();
			record = highscore.getRecord(id);
			str = new String(record);
			// Operate with str to extract name and points
		}
		highscore.closeRecordStore();
		if (str != null)
			best = Integer.parseInt(str);
		else
			best = 0;
	}
}
